# 导入turtle工具包
from turtle import *

# 输赢
win = False

# 最快速度
speed(0)
# 背景颜色
bgcolor("lightgreen")
# 颜色变量，默认是黑色
yanse = "black"
# 格子大小
gz = 40

judge = Turtle()
judge.up()
judge.goto(-460, 330)
judge.write("谁下", font=("Kai", 40, "bold"))
judge.color(yanse)
judge.goto(-420, 300)
judge.dot(30)

# 画19条横线
for i in range(19):
    up()
    goto(-gz * 9, gz * (9 - i))
    down()
    fd(gz * 18)
    bk(gz * 18)

rt(90)
# 画19条竖线
for i in range(19):
    up()
    goto(-gz * (9 - i), gz * 9)
    down()
    fd(gz * 18)
    bk(gz * 18)

# 画最外面的粗边框
pensize(5)
for i in range(4):
    fd(gz * 18)
    rt(90)

# 2维列表，存储棋盘棋子数据，0表示没有棋子，1表示黑棋，2表示白棋
m = [[0] * 19 for i in range(19)]

# 判断输赢
def check(i, j):
    global win
    g = [0] * 8
    fw = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1))
    for index in range(8):
        d = fw[index]
        next_i = i + d[0]
        next_j = j + d[1]
        while next_i in range(19) and next_j in range(19) and m[next_i][next_j] == m[i][j]:
            g[index] = g[index] + 1
            next_i = next_i + d[0]
            next_j = next_j + d[1]

    for index in range(4):
        if g[index] + g[index + 4] + 1 >= 5:
            win = True
            goto(0, 0)
            if yanse == "black":
                write('黑方胜', font=('', 100, ''), align='center')
            else:
                write('白方胜', font=('', 100, ''), align='center')
            break


# 放棋子的自定义函数
def play(x, y):
    if not win:
        global yanse
        global gz
        color(yanse)
        up()
        # x变成格子的整数倍，个位数上要进行4舍5入
        x = round(x / gz) * gz
        # y变成格子的整数倍，个位数上要进行4舍5入
        y = round(y / gz) * gz
        # 计算棋子在电脑中的位置
        i = int(9 - y / gz)
        j = int(x / gz + 9)

        # 只有棋子在棋盘范围内才可以下
        if 0 <= i <= 18 and 0 <= j <= 18:
            # 如果[i, j]没有棋子
            if m[i][j] == 0:
                goto(x, y)
                dot(30)

                if yanse == "black":
                    m[i][j] = 1
                    # 判断输赢
                    check(i, j)
                    yanse = "white"
                else:
                    m[i][j] = 2
                    # 判断输赢
                    check(i, j)
                    yanse = "black"

                # 裁判变色并重新画点
                judge.color(yanse)
                judge.dot(30)


# 当鼠标左键点击屏幕时执行放棋子的自定义函数
onscreenclick(play, 1)
done()
